package com.travel.server.manager.impl.dal;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.MessageTemplateDao;
import com.travel.server.manager.model.MessageTemplate;
import com.travel.server.manager.model.MessageTemplateExample;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:26
 * @Desc:
 */
@Component
@Slf4j
public class MessageDal {

    @Autowired
    private MessageTemplateDao messageTemplateDao;

    public List<MessageTemplate> selectByExample(MessageParam param){
        MessageTemplateExample example = makeExample(param);

        return messageTemplateDao.selectByExample(example);
    }

    public PageDto<MessageTemplate> select(MessageParam param){
        MessageTemplateExample example = makeExample(param);
        int total = messageTemplateDao.countByExample(example);
        example.setOrderByClause("update_time desc");
        List<MessageTemplate> list = messageTemplateDao.selectByExample(example);
        PageDto<MessageTemplate> pageDto = new PageDto<>();
        pageDto.setTotal(total);
        pageDto.setList(list);
        return pageDto;
    }

    public void inserOrUpdate(MessageTemplate messageTemplate){
        if(messageTemplate.getId() == null){
            messageTemplateDao.insertSelective(messageTemplate);
        }else {
            messageTemplateDao.updateByPrimaryKey(messageTemplate);
        }
    }

    public MessageTemplate selectByTemplateId(String templateId){
        MessageParam param = new MessageParam();
        param.setTemplateId(templateId);
        List<MessageTemplate> list = selectByExample(param);
        return CollectionUtil.getFirst(list);
    }

    public MessageTemplate selectById(Integer id){
        MessageTemplate messageTemplate = messageTemplateDao.selectByPrimaryKey(id);
        if(messageTemplate == null){
            log.error("消息模板不存在, id {}", id);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        return messageTemplate;
    }

    private MessageTemplateExample makeExample(MessageParam param){
        MessageTemplateExample example = new MessageTemplateExample();
        MessageTemplateExample.Criteria criteria = example.or();
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        if(param.getType() != null){
            criteria.andMessageTypeEqualTo(param.getType());
        }
        if(StringUtils.isNotBlank(param.getTemplateId())){
            criteria.andSmsTemplateIdEqualTo(param.getTemplateId());
        }
        if(param.getOffset() != null && param.getLimit() != null){
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }

        return example;
    }

    @Data
    public static class MessageParam{

        private Integer state = 1;

        private Integer examState;

        private String templateId;

        private Integer offset;

        private Integer limit;

        private Integer type;
    }
}
